var CONFIG = {
  VERSION: '1.0',
  API_BASE_URL: 'https://api.thecover.cn/enews/', // TESTING
  // API_BASE_URL: '', // PRODUCTION
  apis: function () {
    return {
      'GET_HOT_NEWS': this.API_BASE_URL + 'getHotNews', // 大家都在看
      'GET_HOT_SUBJECT': this.API_BASE_URL + 'getHotSubject', // 热门专栏
      'GET_BANNER_LIST': this.API_BASE_URL + 'getBannerList' // 封面速递
    };
  }
};
var HELPERS = {
  '$get': function (url) {
    var $d = jQuery.Deferred();
    $.ajax({
      type: 'GET',
      url: url,
      dataType: 'json',
      success: function (result) {
        console.log('AJAX success');
        if (parseInt(result.status) === 0 || parseInt(result.status) === 200) {
          $d.resolve(result.data);
        } else {
          $d.reject(result.status, result.message, result.data);
        }
      },
      error: function (xhr, type) {
        console.log('AJAX error ==> xhr: ', JSON.stringify(xhr));
        console.log('AJAX error ==> type: ', JSON.stringify(type));
        $d.reject(444, '网络请求失败~');
      }
    });
    return $d.promise();
  },
  'getHotNews': function () {
    return this.$get(CONFIG.apis().GET_HOT_NEWS);
  },
  'getHotSubject': function () {
    return this.$get(CONFIG.apis().GET_HOT_SUBJECT);
  },
  'getBannerList': function () {
    return this.$get(CONFIG.apis().GET_BANNER_LIST);
  },
  'convert_time': function (timestamp) {
    var day_in_milliseconds = 8.64e7;
    var pad = function (n) {
      return n < 10 ? '0' + n : n
    };
    if (!timestamp) {
      return '遥远的过去';
    }
    var tzoffset = (new Date()).getTimezoneOffset() * 6e4; //offset in milliseconds
    // var cur_time_with_offset = new Date(Date.now() - tzoffset);
    var cur_time = new Date(Date.now());
    var tmp_date_is = new Date(timestamp);
    if (cur_time.getYear() !== tmp_date_is.getYear()) {
      if (cur_time.getYear() - tmp_date_is.getYear() <= 1) {
        return (pad(tmp_date_is.getMonth() + 1) + '-' + pad(tmp_date_is.getDate()));
      }
      return (cur_time.getYear() - tmp_date_is.getYear()) + '年前';
    } else if (cur_time.getMonth() !== tmp_date_is.getMonth()) {
      if (parseInt((cur_time.getTime() - tmp_date_is.getTime()) / day_in_milliseconds) <= 30) {
        if(parseInt((cur_time.getTime() - tmp_date_is.getTime()) / day_in_milliseconds) == 0) {
          if (cur_time.getHours() !== tmp_date_is.getHours()) {
            return (Math.abs(cur_time.getHours() - tmp_date_is.getHours())) + '小时前';
          } else if (cur_time.getMinutes() !== tmp_date_is.getMinutes()) {
            return (Math.abs(cur_time.getMinutes() - tmp_date_is.getMinutes())) + '分钟前';
          } else if (cur_time.getSeconds() !== tmp_date_is.getSeconds()) {
            return (Math.abs(cur_time.getSeconds() - tmp_date_is.getSeconds())) + '秒前';
          } else {
            return '刚刚';
          }
        }
        return Math.abs(parseInt((cur_time.getTime() - tmp_date_is.getTime()) / day_in_milliseconds)) + '天前';
      }
      return (pad(tmp_date_is.getMonth() + 1) + '-' + pad(tmp_date_is.getDate()));
    } else if (cur_time.getDate() !== tmp_date_is.getDate()) {
      if ((cur_time.getDate() - tmp_date_is.getDate()) == 1) {
        return '昨天';
      } else if ((cur_time.getDate() - tmp_date_is.getDate()) == 2) {
        return '前天';
      } else {
        return (Math.abs(cur_time.getDate() - tmp_date_is.getDate())) + '天前';
      }
    } else if (cur_time.getHours() !== tmp_date_is.getHours()) {
      return (cur_time.getHours() - tmp_date_is.getHours()) + '小时前';
    } else if (cur_time.getMinutes() !== tmp_date_is.getMinutes()) {
      return (cur_time.getMinutes() - tmp_date_is.getMinutes()) + '分钟前';
    } else if (cur_time.getSeconds() !== tmp_date_is.getSeconds()) {
      return (cur_time.getSeconds() - tmp_date_is.getSeconds()) + '秒前';
    } else {
      return '刚刚';
    }
  },
  'convert_count': function (num) {
    if (parseInt(num) < 10000) {
      return parseInt(num);
    } else {
      return ((parseInt(num) / 10000).toFixed(1)) + '万';
    }
  },
  'paddingResult': function (type, obj) {
    var self = this;
    var key_name_of_list = '';
    if (type === 'GET_HOT_NEWS') {
      key_name_of_list = 'list'; // 大家都在看
    } else if (type === 'GET_HOT_SUBJECT') {
      key_name_of_list = 'sub_list'; // 热门专栏
    } else {
      key_name_of_list = 'banner'; // 封面速递
    }
    $.each(obj[key_name_of_list], function (index, value) {
      if (value.hasOwnProperty('label') && parseInt(value.label) === 10) {
        value.label = '广告';
      } else {
        value.label = '';
      }
      if (value.hasOwnProperty('date_publish')) {
        value.date_publish = self.convert_time(value.date_publish);
      }
      if (value.hasOwnProperty('happen_time')) {
        value.date_publish = self.convert_time(value.happen_time);
      }
      if (value.hasOwnProperty('review_count')) {
        value.review_count = self.convert_count(value.review_count);
      }
    });
    return obj;
  }
};
